//
//  HRDynamicCommentTableViewCell.m
//  Hear
//
//  Created by mac on 2017/4/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicCommentTableViewCell.h"
#import "HRDynamicCommentModel.h"
#import "NSMutableAttributedString+GDAttString.h"
#import "HRCoverTimeModel.h"
#import "HRCoverImageModel.h"
#import "HRDynamicInfoCommentView.h"
@interface HRDynamicCommentTableViewCell()
@property (nonatomic, strong)UIView *conteView;
@property (nonatomic, strong)UIImageView *iconImageViw;
@property (nonatomic, strong)YYLabel *nickNameLabel;
@property (nonatomic, strong)UILabel *timeLabel;
@property (nonatomic, strong)YYLabel *commentLabel;
@property (nonatomic, strong)UILabel *lineLabel;
@property(nonatomic, copy)NSString *commentText;
@property(nonatomic, strong)HRDynamicInfoCommentView *commentView;

@end
@implementation HRDynamicCommentTableViewCell

-(void)setCommentModel:(HRDynamicCommentModel *)commentModel{
    _commentModel = commentModel;
    WeakSelf
    
    //self.iconImageViw.image = [HRCoverImageModel OriginImage:[UIImage imageNamed:commentModel.CommentUserPictureName] scaleToSize:CGSizeMake(40, 40)];
    
    if (commentModel.CommentUserPictureBackgroundColor.length){
        [HRCoverImageModel OriginImage:[UIImage imageNamed:commentModel.CommentUserPictureName] scaleToSize:CGSizeMake(40, 40) callBackOriginImage:^(UIImage *originImage) {
            weakSelf.iconImageViw.image = originImage;
        }];
        self.iconImageViw.backgroundColor = [UIColor colorWithHexString:commentModel.CommentUserPictureBackgroundColor];
    }else{
        [self.iconImageViw.layer setImageWithURL:[NSURL URLWithString:commentModel.CommentUserPictureName] placeholder:[UIImage imageNamed:@""]];
    }
    
    
    self.iconImageViw.backgroundColor = [UIColor colorWithHexString:commentModel.CommentUserPictureBackgroundColor];
    
    self.timeLabel.text = [HRCoverTimeModel covetTime:commentModel.CreateTime];
    
    NSMutableAttributedString *attNickname = [NSMutableAttributedString attStringFromTextForChatting:commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.CommentUserNickName textFont:CustomUIFont(13)];
    
    [attNickname setColor:SecondTextColor range:NSMakeRange(0, (commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.CommentUserNickName).length)];

    
    self.nickNameLabel.attributedText = attNickname;
    
    

    

//    NSMutableAttributedString *attContent;
//    NSString *contentStr = @"";
//
//
//    if (commentModel.ReplyUserLocalId.length){
//
//        contentStr = [NSString stringWithFormat:@"回复%@：%@",commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.ReplyUserNickName,commentModel.CommentText];
//
//        attContent = [NSMutableAttributedString arrtLabelText:[NSString stringWithFormat:@"回复%@：%@",commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.ReplyUserNickName,commentModel.CommentText] arrFont:CustomUIFont(14) attColor:MainColor lineLengh:3 alignment:0 colorLenghFrom:2 colorLengh:(commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.ReplyUserNickName).length];
//    }else{
//        attContent = [NSMutableAttributedString attStringFromTextForChatting:commentModel.CommentText textFont:CustomUIFont(14)];
//
//
//        contentStr = commentModel.CommentText;
//
//    }
//
//    if ([_commentModel.CommentType isEqualToString:@"2"]){
//        _soundButton.commentModel = commentModel;
//
//        if (commentModel.ReplyUserLocalId.length){
//            attContent = [NSMutableAttributedString arrtLabelText:[NSString stringWithFormat:@"语音回复%@",commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.ReplyUserNickName] arrFont:CustomUIFont(14) attColor:NavTinColor lineLengh:3 alignment:0 colorLenghFrom:4 colorLengh:commentModel.ReplyUserNickName.length];
//
//            contentStr = [NSString stringWithFormat:@"语音回复%@",commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.ReplyUserNickName];
//        }
//
//
//    }
//    self.commentText= contentStr;
//
//    [attContent setColor:SecondTextColor range:NSMakeRange(0, contentStr.length)];
    NSMutableAttributedString *attContent;

    
    if ([commentModel.CommentLevel intValue]==3){

        self.commentText = [NSString stringWithFormat:@"回复%@：%@",commentModel.ReplyUserNickName,commentModel.CommentText];
        
       

        attContent = [NSMutableAttributedString arrtLabelText:self.commentText arrFont:CustomUIFont(14) attColor:SecondTextColor lineLengh:8 alignment:0 colorLenghFrom:0 colorLengh:self.commentText.length];
        
        [attContent setColor:MainColor range:NSMakeRange(2, commentModel.ReplyUserNickName.length)];


    }else{
        self.commentText = commentModel.CommentText;
        attContent = [NSMutableAttributedString arrtLabelText:self.commentText arrFont:CustomUIFont(14) attColor:SecondTextColor lineLengh:8 alignment:0 colorLenghFrom:0 colorLengh:commentModel.CommentText.length];
    }
    _soundButton.hidden = YES;
    if ([commentModel.CommentType intValue] ==2){
        _soundButton.commentModel = commentModel;
        _soundButton.hidden = NO;
    }

    self.commentLabel.attributedText = attContent;

    self.commentView.commentModel = commentModel;
    
    [self updataFrame];

    
}

- (void)updataFrame{
//    CGFloat leftPading = 15;

    
    NSString *contentText = self.commentText;
    
    if ([_commentModel.CommentType isEqualToString:@"2"]){// 语音
        if (_commentModel.ReplyUserLocalId.length){
            _commentLabel.height = 20;
            _soundButton.frame = CGRectMake(self.nickNameLabel.left,  _commentLabel.bottom+10, 150, 50);

        }else{
            _commentLabel.height = 0;
            _soundButton.frame = CGRectMake(self.nickNameLabel.left,  _commentLabel.bottom, 150, 50);

        }
        self.commentView.height = [HRDynamicInfoCommentView sizeDynamicInfoCommentViewCount:_commentModel.ChildComments.count].height;
        if (!self.commentModel.ChildComments.count){
            self.commentView.height = 0;
        }
        self.commentView.top = _soundButton.bottom + 15;

        _lineLabel.top = self.commentView.bottom + 15;
        _soundButton.hidden = NO;
    }else{
        _soundButton.hidden = YES;
        if(contentText.length == 0) return;
        
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.lineSpacing = 8;
        NSDictionary *dic = @{NSFontAttributeName:CustomUIFont(14), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f};
        
        
       _commentLabel.height = [contentText boundingRectWithSize:CGSizeMake(self.width-self.nickNameLabel.left-10, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height+20;

        _commentLabel.width = [contentText boundingRectWithSize:CGSizeMake(self.width-self.nickNameLabel.left-10, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.width;

        
        self.commentView.height = [HRDynamicInfoCommentView sizeDynamicInfoCommentViewCount:_commentModel.ChildComments.count].height;
        self.commentView.hidden = NO;
        if (!self.commentModel.ChildComments.count){
            self.commentView.height = 0;
            self.commentView.hidden = YES;
        }
        self.commentView.top = _commentLabel.bottom + 15;
        
        _lineLabel.top = self.commentView.bottom + 15;
        

    }
    
    
    _commentModel.cellHeight = _lineLabel.bottom;
    
    
    self.conteView.height = _lineLabel.bottom;
}


+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"HRDynamicCommentTableViewCell";
    
    HRDynamicCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(!cell){
        cell = [[HRDynamicCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self)
    {
        WeakSelf
        self.conteView = [UIView new];
        [self addSubview:self.conteView];
        self.conteView.userInteractionEnabled = YES;
        self.conteView.frame = CGRectMake(0, 0, kFullWidth, 40);
        
        
        self.iconImageViw = [UIImageView new];
        [self.conteView addSubview:self.iconImageViw];
        self.iconImageViw.layer.cornerRadius = 20;
        self.iconImageViw.clipsToBounds = YES;
        CGFloat leftPading = 15;
        self.iconImageViw.userInteractionEnabled = YES;
        self.iconImageViw.frame = CGRectMake(leftPading, leftPading, 40, 40);
        UITapGestureRecognizer *iconTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            if (weakSelf.iconTapBock){
                weakSelf.iconTapBock();
            }
        }];
        [self.iconImageViw addGestureRecognizer:iconTap];
        
        
        self.nickNameLabel = [YYLabel new];
        self.nickNameLabel.textColor = FirstTextColor;
        self.nickNameLabel.font = CustomUIFont(13);
        [self.conteView addSubview:self.nickNameLabel];
        self.nickNameLabel.frame = CGRectMake(self.iconImageViw.right+leftPading, self.iconImageViw.centerY-10, 200, 20);
                              
        self.timeLabel = [UILabel new];
        self.timeLabel.textColor = SecondTextColor;
        self.timeLabel.font = CustomUIFont(11);
        [self.conteView addSubview:self.timeLabel];
        self.timeLabel.frame = CGRectMake(kFullWidth-leftPading-100, self.nickNameLabel.top, 100, self.nickNameLabel.height);
        self.timeLabel.textAlignment = 2;
        
        self.commentLabel = [YYLabel new];
        self.commentLabel.textColor = SecondTextColor;
        self.commentLabel.font = CustomUIFont(14);
        [self.conteView addSubview:self.commentLabel];
        self.commentLabel.frame = CGRectMake(self.nickNameLabel.left, self.iconImageViw.bottom+leftPading, kFullWidth-leftPading-self.nickNameLabel.left, 20);
        self.commentLabel.numberOfLines = 0;
        
        
        self.commentLabel.userInteractionEnabled = YES;
        [self.commentLabel addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
        
        
        _soundButton = [HRSoundButton buttonWithType:0];
        [self.conteView addSubview:_soundButton];
        
        
        _soundButton.playCommentMedioBlock = ^(HRDynamicCommentModel *currentCommentModel){
            if (weakSelf.playCommentMedioBlock){
                weakSelf.playCommentMedioBlock(currentCommentModel);
            }
        };
        
        
        HRDynamicInfoCommentView *commentView = [HRDynamicInfoCommentView new];
        commentView.backgroundColor = CustomColor(244, 244, 244, 1);
        [self.conteView addSubview:(self.commentView = commentView)];
        commentView.frame = CGRectMake(self.nickNameLabel.left, self.commentLabel.bottom+leftPading, kFullWidth-leftPading-self.nickNameLabel.left, 0);
        commentView.userInteractionEnabled = YES;
        UITapGestureRecognizer *commentTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentClick)];
        [commentView addGestureRecognizer:commentTap];
        
        self.lineLabel = [UILabel new];
        self.lineLabel.backgroundColor = CustomColor(240, 240, 240, 1);
        [self.conteView addSubview:self.lineLabel];
        self.lineLabel.frame = CGRectMake(self.commentLabel.left, self.commentLabel.bottom+leftPading,self.commentLabel.width , 0.7);
        
    }
    return self;
}

- (void)commentClick{
    if (self.commentClickBlock){
        self.commentClickBlock();
    }
}


- (void)longTap:(UILongPressGestureRecognizer *)longRecognizer
{
    //判断 如果当前是长按状态
    if (longRecognizer.state==UIGestureRecognizerStateBegan)
    {
        [self becomeFirstResponder];
        //创建一个菜单控制器  单例
        UIMenuController *menu=[UIMenuController sharedMenuController];
        //        //创建两个菜单按钮
        //        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(copyItemClicked:)];
        
        UIMenuItem *resendItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyItemClicked:)];
        //添加到控制器上
        [menu setMenuItems:[NSArray arrayWithObjects:resendItem,nil]];
        //菜单的位置  在cell 的边界
        CGRect rect = CGRectMake(self.commentLabel.centerX, self.commentLabel.top,50, 1);
        [menu setTargetRect:rect inView:self];
        //菜单的显示
        [menu setMenuVisible:YES animated:YES];
    }
}

-(BOOL)canBecomeFirstResponder{
    
    return YES;
    
}
// 用于UIMenuController显示，缺一不可
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    if (action ==@selector(copyItemClicked:)){
        
        return YES;
        
    }
    
    return NO;//隐藏系统默认的菜单项
}

-(void)copyItemClicked:(id)sender{
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    if (self.commentText.length != 0)
    {
        [pasteboard setString:self.commentText];
    }
    
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    UIColor *backgroundColor = self.iconImageViw.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.iconImageViw.backgroundColor = backgroundColor;
    

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    UIColor *backgroundColor = self.iconImageViw.backgroundColor;

    self.iconImageViw.backgroundColor = backgroundColor;

    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
